<template>
    <view class="container">
        <header-bar :config="config"></header-bar>
        <view class="main">
            <view class="price-box" @click="receiptMoneyFn()">
                <view class="price">
                    <text class="price-text"
                        ><text class="price-symbol">￥</text>{{ Number(receiptmoney).toFixed(2) }}</text
                    >
                    <uni-icons
                        class="icon"
                        size="48"
                        type="compose"
                        v-if="OrderDetail.State == 6 || OrderDetail.State == 15"></uni-icons>
                </view>
                <view class="title">收款金额</view>
            </view>
            <view class="action">
                <button class="btn btn-main" @click="$refs.popTip.open()" v-if="false">现金收款</button>
                <invite-pay :data="OrderDetail"></invite-pay>
                <button class="btn btn-main-hollow" v-if="OrderDetail.IsCashDelivery" @click="$refs.tip.open()">
                    货到付款
                </button>
            </view>
        </view>
        <uni-popup ref="popReceiptMoney">
            <uni-popup-dialog
                mode="input"
                inputType="digit"
                v-model="Number(receiptmoney).toFixed(2)"
                title="修改收款金额"
                :before-close="true"
                @close="close"
                @confirm="confirm"></uni-popup-dialog>
        </uni-popup>
        <uni-popup ref="popTip">
            <uni-popup-dialog
                title="温馨提示"
                :content="`现金收款￥${Number(receiptmoney).toFixed(2)},是否确认收款？`"
                :before-close="true"
                @close="closeTip"
                @confirm="confirmTip"></uni-popup-dialog>
        </uni-popup>
    </view>
</template>

<script>
import { mapMutations, mapState } from "vuex"
import invitePay from "@/components/order/invite-pay.vue"
import { OrderReceipt, DiscountMoney } from "@/service/api/order.js"
export default {
    components: {
        invitePay,
    },
    data() {
        return {
            config: {
                title: "收款",
                centerSlot: true,
            },
            OrderDetail: null,
            orderId: 0,
            receiptmoney: 0,
        }
    },
    onLoad(e) {
        this.orderId = e.orderId
        this.OrderDetail = JSON.parse(decodeURIComponent(e.orderData))
        console.log(123)
        console.log(this.OrderDetail)

        // let money = this.OrderDetail.ProductPrice + this.OrderDetail.PostagePrice - this.OrderDetail.PolicyDiscount- this.OrderDetail.CouponMoney - this.OrderDetail.ReceiptMoney
        // if(money<0){
        // 	money=0
        // }
        this.receiptmoney = this.OrderDetail.ArrearsPrice
    },
    mounted() {},
    onShow() {},
    methods: {
        ...mapMutations(["UP_ORDER_VIEW"]),
        orderReceipt() {
            let param = {
                orderId: this.orderId,
                receiptmoney: this.receiptmoney,
            }
            let path = `?orderId=${this.orderId}&receiptmoney=${this.receiptmoney}`
            OrderReceipt(param, path).then((res) => {
                if (res.State == 1) {
                    this.easyGoBack()
                } else {
                    this.easyToast(res.Msg)
                }
            })
        },
        close() {
            this.$refs.popReceiptMoney.close()
        },
        closeTip() {
            this.$refs.popTip.close()
        },
        confirm(e) {
            if (!e || e.toString().trim() == "") {
                this.easyToast("请输入正确的金额")
                return
            }
            if (e < 0) {
                this.easyToast("请输入正确的金额")
                return
            }

            let param = {
                orderId: this.orderId,
                paymoney: e,
            }
            let path = "?orderId=" + this.orderId + "&paymoney=" + e
            DiscountMoney(param, path).then((res) => {
                if (res.State == 1) {
                    this.receiptmoney = e
                    this.easyToast("修改成功")
                    this.upOrderView()
                } else {
                    this.easyToast(res.Msg)
                }
            })
            this.close()
        },
        confirmTip(e) {
            this.orderReceipt()
            this.closeTip()
        },
        upOrderView() {
            this.UP_ORDER_VIEW(new Date().getTime())
        },
        receiptMoneyFn() {
            if (this.OrderDetail.State == 6 || this.OrderDetail.State == 15) {
                this.$refs.popReceiptMoney.open()
            }
        },
    },
}
</script>

<style lang="scss" scoped>
.price-box {
    text-align: center;
    margin: 120rpx 0 100rpx;
    .price {
        display: flex;
        justify-content: center;
        align-items: center;
        .price-text {
            font-weight: 700;
            font-size: 80rpx;
            .price-symbol {
                font-size: 48rpx;
            }
        }
        .icon {
            margin-left: 16rpx;
            color: #ccc;
        }
    }
    .title {
        font-weight: 500;
        color: $color-gray-2;
        font-size: $font-size-28;
    }
}
.action {
    .btn {
        width: 300rpx;
        height: 80rpx;
        margin-bottom: 40rpx;
    }
}
</style>
